/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controle;

import Classes.Cliente;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

/**
 *
 * @author Rafael
 */
public class ControleCliente {

    Conexao cx;
    Connection con;

    public ControleCliente() throws SQLException {
        cx = new Conexao();
        con = cx.getConnection();
    }

    public void adicionaCliente(Cliente c) throws SQLException {

        String nome = c.getNome();
        String telefone = c.getTelefone();
        String cpf = c.getCpf();
        Integer status_cliente = c.getStatusCliente();
        //Date dataCadastro;

        String querry = "INSERT INTO cliente(nome, cpf, telefone, status_cliente) VALUES(?, ?, ?, ?)";
        PreparedStatement ps = con.prepareStatement(querry);

        ps.setString(1, nome);
        ps.setString(2, cpf);
        ps.setString(3, telefone);
        ps.setInt(4, status_cliente);


        ps.executeUpdate();

    }

    public ArrayList listaClientes() {
        ArrayList<Cliente> clientes = new ArrayList();
        Cliente c;
        try {
            Statement stm = con.createStatement();
            ResultSet rs = stm.executeQuery("SELECT * FROM cliente");
            while (rs.next()) {
                c = new Cliente(Integer.parseInt(rs.getString("cod_cliente")), rs.getString("nome"), rs.getString("cpf"), 
                        rs.getString("telefone"));
                clientes.add(c);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return clientes;
    }
    
    public ArrayList listaClientesPendencia()
    {
        ArrayList<Cliente> clientesp = new ArrayList();
        Cliente c;
        try {
            Statement stm = con.createStatement();
            ResultSet rs = stm.executeQuery("SELECT * FROM cliente WHERE status_cliente = 2");
            while (rs.next()) {
                c = new Cliente(Integer.parseInt(rs.getString("cod_cliente")), rs.getString("nome"), rs.getString("cpf"), 
                        rs.getString("telefone"));
                clientesp.add(c);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }        
        return clientesp;
    }

    public void closeConnection() {
        try {
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
